package org.smartcti.freeswitch.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Some static utility methods to imitate FreeSwitch specific logic. <p/> See
 * FreeSwitch's <code>util.c</code>. <p/> Client code is not supposed to use
 * this class.
 * 
 * @author srt
 * @version $Id: AstUtil.java,v 1.3 2008/12/25 06:16:33 rason.yang Exp $
 */
public class ConvertUtil {

	private static final Set<String> TRUE_LITERALS;
	private static final Set<String> NULL_LITERALS;
	private static final DateFormat localFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	static {
		TRUE_LITERALS = new HashSet<String>(20);
		TRUE_LITERALS.add("yes");
		TRUE_LITERALS.add("true");
		TRUE_LITERALS.add("y");
		TRUE_LITERALS.add("t");
		TRUE_LITERALS.add("1");
		TRUE_LITERALS.add("on");
		TRUE_LITERALS.add("enabled");

		NULL_LITERALS = new HashSet<String>(20);
		NULL_LITERALS.add("<unknown>");
		NULL_LITERALS.add("unknown");
		NULL_LITERALS.add("none"); // VarSet event in pbx.c
		NULL_LITERALS.add("<none>");
		NULL_LITERALS.add("-none-");
		NULL_LITERALS.add("(none)");
		NULL_LITERALS.add("<not set>");
		NULL_LITERALS.add("(not set)");
		NULL_LITERALS.add("<no name>");
		NULL_LITERALS.add("n/a"); // channel in AgentsEvent
		NULL_LITERALS.add("<null>");
		NULL_LITERALS.add("(null)"); // appData in ListDialplanEvent
	}

	public static Boolean toBoolean(String source) {
		if (source == null) {
			return false;
		}
		if (TRUE_LITERALS.contains(source.trim())) {
			return true;
		} else {
			return false;
		}
	}
	
	public static Integer toInteger(String source) {
		if (source == null) {
			return null;
		}
		try {
			return Integer.parseInt(source);
		} catch (Exception e) {
			return null;
		}
	}
	
	public static Date toDate(String source) {
		if (source == null) {
			return null;
		}
		try {
			final Date date = localFormat.parse(source);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	
	
	public static Map<String,String> toMap(Map<String, String> attributes) {
		if (attributes == null) {
			return null;
		}
		return null;
	}
}
